<%+cbi/valueheader%>
<script src="/jd-dailybonus/jquery.min.js"></script>
<style>
	.modals-bg {
		position: fixed;
		z-index: 999;
		width: 100%;
		height: 100%;
		left: 0;
		top: 0;
		background: rgba(255, 255, 255, 0.8);
		display: none;
	}

	.modals {
		position: fixed;
		z-index: 100;
		width: 60%;
		height: 500px;
		background: #172b4d;
		left: 20%;
		top: 15%;
		color: #fff;
		border-radius: 10px;
		padding: 20px;

		box-sizing: border-box;
		-moz-box-sizing: border-box;
		/* Firefox */
		-webkit-box-sizing: border-box;
		/* Safari */
	}

	.modals h2 {
		color: #fff;
		background: transparent;
		padding: 0 !important;
	}

	.modals h3 {
		font-size: 14px;
		color: #f5365c !important;
		background: transparent;
		margin: 0 0 1em 0;
		padding: 0 0 0.5em 0;
	}

	#log_content3 {
		border: 0;
		width: 99%;
		height: calc(100% - 4rem);
		font-family: 'Lucida Console';
		font-size: 11px;
		background: transparent;
		color: #FFFFFF;
		outline: none;
		padding-left: 3px;
		padding-right: 22px;
		overflow: hidden
	}

	.cbi-value-version {
		word-wrap: break-word;
		font-size: 0.875rem;
		line-height: 1.6;
		color: #5e72e4;
		font-weight: bold;
		padding: .7rem;
		padding-left: 0;
		width: 23rem;
		float: left;
		text-align: left;
		display: table-cell;
	}

	@media screen and (max-width: 1024px) {
		.modals {
			position: fixed;
			z-index: 100;
			width: 80%;
			height: 500px;
			background: #172b4d;
			left: 10%;
			top: 15%;
			color: #fff;
			border-radius: 10px;
			padding: 20px;
		}
	}

	@media screen and (max-width: 700px) {
		.modals-bg {
			position: fixed;
			z-index: 100000;

		}

		.modals {
			width: 100%;
			height: 100%;
			left: 0;
			top: 0;
		}
	}

</style>
<label class="cbi-value-title"><%= translate("Run") %></label>
<div class="cbi-value-field">
	<input class="cbi-button cbi-button-reload" id="update_service" type="button" size="0"
		value="<%= translate("Save Cookies And Run Service") %>">
</div>

<%+cbi/valuefooter%>
<%+cbi/valueheader%>
<label class="cbi-value-title"><%= translate("Current Script Version") %></label>
<div class="cbi-value-field">
	<div class="cbi-value-version">
		v<%=self.version%>
	</div>

</div>
<%+cbi/valuefooter%>

<%+cbi/valueheader%>
<label class="cbi-value-title"><%= translate("Update Script") %></label>
<div class="cbi-value-field">
	<input class="cbi-button cbi-button-reload" id="update_script" type="button" size="0"
		value="<%= translate("Check Script Version") %>">

</div>
<%+cbi/valuefooter%>

<script type="text/javascript">

	const SAVE_URL = '<%=luci.dispatcher.build_url("admin", "services", "jd-dailybonus","run")%>';
	const CHECK_URL = '<%=luci.dispatcher.build_url("admin", "services", "jd-dailybonus","check_update")%>';
	const UPDATE_URL = '<%=luci.dispatcher.build_url("admin", "services", "jd-dailybonus","update")%>';
	const CHECKING_TEXT = '<%= translate("Checking the New Version ...") %>';
	const UPDATING_TEXT = '<%= translate("Updating script,please wait ...") %>';
	const NEW_VERSION = '<%= translate("Is currently the latest version") %>';
	const UPDATE_TEXT = '<%= translate("There is a new version, click to update") %>';

	var needUpdate = false;
	var newVersion;
	var _responseLen;
	var noChange = 0;
	var modal = '<div class="modals-bg">' +
		'<div class="modals">' +
		'<h2><%:Sign in info%></h2>' +
		'<h3 style="margin-left:0;"><%:Service is running,Please do not refresh!%></h3>' +
		'<textarea cols="63" rows="28" wrap="on" readonly="readonly" id="log_content3" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"></textarea>' +
		'</div>' +
		'</div>';

	//显示并开始刷新订阅
	function update_service() {
		$("body").append(modal);
		$(".modals-bg").show();
		setTimeout("get_realtime_log();", 500);
	}
	//保存订阅按钮
	$("#update_service").click(function () {
		prefix_array = $("#cbi-jd-dailybonus-global .cbi-section-node").attr("id").split("-");
		prefix = prefix_array[prefix_array.length - 1];
		//console.log(prefix);
		if ($("[name='cbid.jd-dailybonus." + prefix + ".auto_update']").is(":checked")) {
			var auto_update = "1";
			var auto_update_time = $("[name='cbid.jd-dailybonus." + prefix + ".auto_update_time']").val();
		} else {
			var auto_update = "0";
			var auto_update_time = "0";
		}
		
		if ($("[name='cbid.jd-dailybonus." + prefix + ".auto_run']").is(":checked")) {
			var auto_run = "1";
			var auto_run_time = $("[name='cbid.jd-dailybonus." + prefix + ".auto_run_time']").val();
		} else {
			var auto_run = "0";
			var auto_run_time = "0";
		}
		var stop = $("[name='cbid.jd-dailybonus." + prefix + ".stop']").val();
		var cookies = $("[name='cbid.jd-dailybonus." + prefix + ".cookie']").val();
		var cookies2 = $("[name='cbid.jd-dailybonus." + prefix + ".cookie2']").val();
		var failed=($("[name='cbid.jd-dailybonus." + prefix + ".failed']").is(":checked"))?"1":"0";
		var serverchan = $("[name='cbid.jd-dailybonus." + prefix + ".serverchan']").val();
		var data = {
			auto_update: auto_update,
			auto_update_time: auto_update_time,
			auto_run: auto_run,
			auto_run_time: auto_run_time,
			cookies: cookies,
			cookies2: cookies2,
			stop: stop,
			serverchan: serverchan,
			failed: failed
		}
		//console.log(data);
		$.ajax({
			type: "post",
			url: SAVE_URL,
			dataType: "json",
			data: data,
			success: function (d) {
				if (d.error == 0) {
					update_service();
				} else {
					alert("请填写cookies");
				}
			}
		});
		return false;
	});

	//更新脚本
	$("#update_script").click(function () {
		if (!needUpdate) {
			//获取远端脚本版本
			check_version()
		} else {
			$("#update_script").attr("disabled", true);
			$("#update_script").val(UPDATING_TEXT);
			//console.log(data);
			var data = {
				version: $("#update_script").attr("data-version")
			}
			$.ajax({
				type: "post",
				url: UPDATE_URL,
				dataType: "json",
				data: data,
				success: function (d) {
					if (d.error == 0) {

						$("#update_script").val(NEW_VERSION);
						$(".cbi-value-version").text("v" + newVersion);
					} else {
						$("#update_script").attr("disabled", false);
						alert("更新错误请重试");
					}
				}
			});
		}

		return false;
	});


	function get_realtime_log() {
		$.ajax({
			url: '/JD_DailyBonus.htm?v=' + parseInt(Math.random() * 100000000),
			dataType: 'html',
			error: function (xhr) {
				setTimeout("get_realtime_log();", 1000);
			},
			success: function (response) {
				var retArea = document.getElementById("log_content3");
				if (response.search(" 秒") != -1) {
					noChange++;
				}
				console.log(noChange);
				if (noChange > 10) {
					window.location.href = '<%=luci.dispatcher.build_url("admin", "services", "jd-dailybonus")%>';
					return false;
				} else {
					setTimeout("get_realtime_log();", 250);
				}
				retArea.value = response;
				retArea.scrollTop = retArea.scrollHeight;
				_responseLen = response.length;
			},
			error: function () {
				setTimeout("get_realtime_log();", 500);
			}
		});
	}

	function check_version() {
		$("#update_script").attr("disabled", true);
		$("#update_script").val(CHECKING_TEXT);
		$.ajax({
			url: CHECK_URL,
			dataType: 'json',
			error: function (xhr) {

			},
			success: function (data) {
				if(data.new_version == 501){
					$("#update_script").attr("disabled", false);
					$("#update_script").val("无法获取新版本，请重试！");
				}else if (data.new_version > <%= self.version %>) {
					$("#update_script").attr("disabled", false);
					$("#update_script").val(UPDATE_TEXT + " v" + data.new_version);
					$("#update_script").attr("data-version", data.new_version);
					newVersion = data.new_version;
					needUpdate = true;
				} else {
					$("#update_script").val(NEW_VERSION + " v" + data.new_version);
				}

	}
		});
	}

</script>
